/*
 * String.h
 *
 *  Created on: 13.12.2012
 *      Author: Lapchinskiy
 */

#ifndef STRING_H_
#define STRING_H_
#include "Global_types.h"
/* Exported macro ------------------------------------------------------------*/
/* Common routines */
#define IS_AF(c)             ((c >= 'A') && (c <= 'F'))
#define IS_af(c)             ((c >= 'a') && (c <= 'f'))
#define IS_09(c)             ((c >= '0') && (c <= '9'))
#define ISVALIDHEX(c)        IS_AF(c) || IS_af(c) || IS_09(c)
#define ISVALIDDEC(c)        IS_09(c)
#define CONVERTDEC(c)        (c - '0')

#define CONVERTHEX_alpha(c)  (IS_AF(c) ? (c - 'A'+10) : (c - 'a'+10))
#define CONVERTHEX(c)        (IS_09(c) ? (c - '0') : CONVERTHEX_alpha(c))

#define SerialPutString(x)   Serial_PutString((uint8_t*)(x))
class StringManager {
	private:
public:
	StringManager();
        static const uint16_t MAX_STRING_LENGTH;
        
	static bool Compare(char *str1, char *str2) {
		uint16_t shift = 0;
		while (*(str1 + shift) == *(str2 + shift)) {
			if ((*(str1 + shift+1) == 0) || (*(str2 + shift+1) == 0)) {
				return true;
			}
			if (shift++ > MAX_STRING_LENGTH) {
				break;
			}
		}
		return false;
	}
        static uint8_t getLength(char *str) {
		uint16_t length = 0;
		while (*(str+length)!=0) {
			if (length++ > MAX_STRING_LENGTH) {
				break;
			}
		}
		return length;
	}
        static uint32_t Str2Int(char *inputstr, int32_t *intnum)
        {
          uint32_t i = 0, res = 0;
          uint32_t val = 0;

          if (inputstr[0] == '0' && (inputstr[1] == 'x' || inputstr[1] == 'X'))
          {
            if (inputstr[2] == '\0')
            {
              return 0;
            }
            for (i = 2; i < 11; i++)
            {
              if ((inputstr[i] == '\0')||(inputstr[i] == ' ')||(inputstr[i] == '\r')||(inputstr[i] == '\n'))
              {
                *intnum = val;
                /* return 1; */
                res = 1;
                break;
              }
              if (ISVALIDHEX(inputstr[i]))
              {
                val = (val << 4) + CONVERTHEX(inputstr[i]);
              }
              else
              {
                /* return 0, Invalid input */
                res = 0;
                break;
              }
            }
            /* over 8 digit hex --invalid */
            if (i >= 11)
            {
              res = 0;
            }
          }
          else /* max 10-digit decimal input */
          {
            for (i = 0;i < 11;i++)
            {
              if ((inputstr[i] == '\0')||(inputstr[i] == ' ')||(inputstr[i] == '\r')||(inputstr[i] == '\n'))
              {
                *intnum = val;
                /* return 1 */
                res = 1;
                break;
              }
              else if ((inputstr[i] == 'k' || inputstr[i] == 'K') && (i > 0))
              {
                val = val << 10;
                *intnum = val;
                res = 1;
                break;
              }
              else if ((inputstr[i] == 'm' || inputstr[i] == 'M') && (i > 0))
              {
                val = val << 20;
                *intnum = val;
                res = 1;
                break;
              }
              else if (ISVALIDDEC(inputstr[i]))
              {
                val = val * 10 + CONVERTDEC(inputstr[i]);
              }
              else
              {
                /* return 0, Invalid input */
                res = 0;
                break;
              }
            }
            /* Over 10 digit decimal --invalid */
            if (i >= 11)
            {
              res = 0;
            }
          }

          return res;
        }
        static uint16_t FindCharPosition(char *str, char character, uint16_t entryNum)
        {
        	uint16_t charpos=0;
        	while(entryNum)
        	{
        		if(*(str+charpos)==character)
        		{
        			if(--entryNum==0)
                                  break;
        		}
                        if((*(str+charpos)=='\r')||(*(str+charpos)=='\n')||(*(str+charpos)==0)||(++charpos>=MAX_STRING_LENGTH))
                        {
                          charpos=0;
                          break;
                        }
        	}
        	return charpos;
        }
       static void Int2Str(uint8_t* str, int32_t intnum)
        {
          uint32_t i, Div = 1000000000, j = 0, Status = 0;
 
          for (i = 0; i < 10; i++)
          {
            str[j++] = (intnum / Div) + 48;
            str[j]=0;
            intnum = intnum % Div;
            Div /= 10;
            if ((str[j-1] == '0') & (Status == 0))
            {
              j = 0;
            }
            else
            {
              Status++;
            }
          }
        }
	~StringManager();
};

#endif /* STRING_H_ */
